<?php
    class Model_user extends CI_Model{
        public $username;
        public $password;
        public $nama;
        public $role;
        
        function __construct() {
            parent::__construct();
        }
        function select_all($i=0){
            $query=$this->db->get('user');
            if($query->num_rows()>0){
                if($i==0)
                    return $query->result();
                else if($i==1)
                    return $query->result_array();
            }else{
                return FALSE;
            }
        }
        function select_with_id($id){
            $query=$this->db->get_where('user',array('iduser'=>$id));
            if($query->num_rows()==1){
                return $query->row();
            }else{
                return FALSE;
            }
        }
        function select_with_username($usr){
            $query=$this->db->get_where('user',array('username'=>$usr));
            if($query->num_rows()==1){
                return $query->result_array();
            }else{
                return FALSE;
            }
        }
        function insert($data=''){
            if($data==''){
                $data=array(
                    'username'=>$this->username,
                    'password'=>md5($this->password),
                    'nama'=>$this->nama,
                    'role'=>$this->role
                );
            }
            $this->db->insert('user',$data);
        }
        function login($usr,$pwd){
            $query=$this->db->get_where('user',array('username'=>$usr,'password'=>md5($pwd),'role'=>'admin'));
            if($query->num_rows()==1){
                return 'admin';
            }else{
                $query2=$this->db->get_where('user',array('username'=>$usr,'password'=>md5($pwd),'role'=>'superadmin'));
                if($query2->num_rows()==1){
                    return 'superadmin';
                }else{
                    return FALSE;
                }
            }
        }
        function delete($usr){
            $this->db->delete('user',array('username'=>$usr));
        }
        function update($usr,$data=''){
            if($data==''){
                $data=array(
                    'username'=>$this->username,
                    'password'=>md5($this->password),
                    'nama'=>$this->nama,
                    'role'=>$this->role
                );
            }else{
                $data['password']=md5($data['password']);
            }
            $this->db->where(array('username'=>$usr));
            $this->db->update('user',$data);
        }
    }
?>
